package com.mo.lib.utils

import android.app.Activity
import android.app.ActivityOptions
import android.content.Intent
import android.os.Build
import android.os.Parcelable
import android.view.View
import com.dylanc.longan.intentExtras
import com.dylanc.longan.startActivity
import com.dylanc.longan.topActivity
import java.io.Serializable

/**
 * @ author：mo
 * @ data：2022/4/15:10:35
 * @ 功能：
 */

inline fun <reified T : Activity> startActivity(isFinish: Boolean = false) {
    startActivity<T>()
    if (isFinish) {
        topActivity.finish()
    }
}

inline fun <reified T : Activity> startActivity(pairs: Any, isFinish: Boolean = false) {
    startActivity<T>("extra" to pairs)
    if (isFinish) {
        topActivity.finish()
    }
}
@JvmOverloads
fun startActivity( cls:Class<*>,pairs: Any=false ){
    com.mo.lib.utils.startActivity(cls, block = {
        when (pairs) {
            is String       -> {
                putExtra("extra", pairs)
            }
            is Int          -> {
                putExtra("extra", pairs)
            }
            is Serializable -> {
                putExtra("extra", pairs)
            }
            is Parcelable   -> {
                putExtra("extra", pairs)
            }
        }

    })
    if (pairs is Boolean && pairs) {
        topActivity.finish()
    }
}

fun startActivity( cls:Class<*>,block:Intent.()->Unit={}){
    startActivity(Intent(topActivity,cls,).apply {
        block.invoke(this)
    })
}

fun Activity.intentExtrasString() =  intentExtras("extra", "")
fun Activity.intentExtrasInt() =   intentExtras("extra", -1)
fun Activity.intentExtrasBoolean() = intentExtras("extra", false)
inline fun <reified T> Activity.intentExtrasBean() = intentExtras<T>("extra")

///**
// * 跳界面-转场动画
// *
// * @param mActivity 当前activity
// * @param mIntent   意图
// * @param mView     一般是被点击view，就是动画的起点view
// * @param tag       联动标识
// */
//fun startActivityTransition( mView: View, tag: String) { // 判断版本号，转场动画是在5.0以后添加的，所以它只在21版本以上起作用
//    intent?.let { //设置转场动画，makeSceneTransitionAnimation为固定动画，实现放大效果，
//        // 参数依次为：mActivity=当前activity，mView=当前跳出view，tag=与下一个activity联系的string串
//        //tag的使用，与下一个界面产生联系
//        if (context is Activity && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            context.startActivity(it, ActivityOptions.makeSceneTransitionAnimation(context as Activity, mView, tag).toBundle())
//        } else {
//            context.startActivity(it)
//        }
//    }
//}
///**
// * 转场到动画返回
// *
// * @param mActivity
// */
//fun finishAfterTransition1(mActivity: Activity) {
//    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//        mActivity.finishAfterTransition()
//    } else {
//        mActivity.finish()
//    }
//}